<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="title-container">
        <h3 class="title">在线教育平台 - 教师端</h3>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
        <el-form-item prop="username">
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="用户名"
            name="username"
            type="text"
            tabindex="1"
            autocomplete="on"
            prefix-icon="el-icon-user"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            ref="password"
            v-model="loginForm.password"
            placeholder="密码"
            name="password"
            tabindex="2"
            autocomplete="on"
            show-password
            prefix-icon="el-icon-lock"
            @keyup.enter.native="handleLogin"
          />
        </el-form-item>

        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入用户名'));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store.dispatch('login', this.loginForm)
            .then(() => {
              this.$store.dispatch('getUserInfo')
                .then(userInfo => {
                  // 判断是否为教师或管理员
                  if (userInfo.role !== 1 && userInfo.role !== 2) {
                    this.$message.error('您没有权限登录教师端');
                    this.$store.dispatch('logout');
                    this.loading = false;
                    return;
                  }
                  this.$message({
                    message: '登录成功',
                    type: 'success'
                  });
                  this.$router.push({ path: this.redirect || '/' });
                })
                .catch(() => {
                  this.loading = false;
                });
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #f0f2f5;
  
  .login-card {
    width: 400px;
    padding: 20px;
  }
  
  .title-container {
    text-align: center;
    margin-bottom: 30px;
    
    .title {
      font-size: 24px;
      color: #409EFF;
      margin: 0;
    }
  }
  
  .login-form {
    .el-input {
      height: 40px;
      
      input {
        height: 40px;
      }
    }
  }
}
</style> 